# $Id: Lists.pm 36 2006-09-29 23:09:30Z cmauch $
# Author: Charles Mauch (cmauch@gmail.com)
# Description: Module to handle email sent from a mailing list
# Legal: (c) 2006 Charles Mauch, released under the terms of the GPLv2 or later

# -- [ sub: CheckLists ] ----------------------------------------------------
#
#Summary:      Subroutine to check emails for mailing list specific header
#              information, and file email appropriately
#Parameters:   none
#Return:       none

sub CheckLists {

    # Looks for the header X-Mailing-List, and if it exists, fire up
    # Email::Address to ensure we strip out comments and descriptions.  This
    # is pretty much the most common mailing list header flag.
    #
    if ( ( Email::Address->parse( $item->header( "X-Mailing-List" ) ) ) ) {
        foreach my $addr ( Email::Address->parse( $item->header( "X-Mailing-List" ) ) ) {
            my $listname = $addr->address;
            Logger( $epochtime, $score, "lists", $listname, $listname, $subject, "lists\/$listname/" );
            $listname = getlistname( $listname );
            $item->accept( "$MauchConfig::maildir/lists/$listname" );
        }
    } ## end if ( ( Email::Address->parse...

    # Looks for the header Mailing-List, and if it exists, fire up
    # Email::Address to ensure we strip out comments and descriptions.
    # Ezmlm uses this format.  So does yahoogroups.
    #
    if ( ( Email::Address->parse( $item->header( "Mailing-List" ) ) ) ) {
        foreach my $addr ( Email::Address->parse( $item->header( "Mailing-List" ) ) ) {
            my $listname = $addr->address;
            Logger( $epochtime, $score, "lists", $listname, $listname, $subject, "lists\/$listname/" );
            $listname = getlistname( $listname );
            $item->accept( "$MauchConfig::maildir/lists/$listname" );
        }
    } ## end if ( ( Email::Address->parse...

    # Looks for the X-BeenThere header, and if it exists, we fire up
    # Email::Address to ensure we strip out comments and descriptions.
    # Most commercial softwares use this, along with majordomo regardless
    # of their individual tweaks and header mangling.  This just works.
    #
    if ( ( Email::Address->parse( $item->header( "X-BeenThere" ) ) ) ) {
        foreach my $addr ( Email::Address->parse( $item->header( "X-BeenThere" ) ) ) {
            my $listname = $addr->address;
            Logger( $epochtime, $score, "lists", $listname, $listname, $subject, "lists\/$listname/" );
            $listname = getlistname( $listname );
            $item->accept( "$MauchConfig::maildir/lists/$listname" );
        }
    } ## end if ( ( Email::Address->parse...

    # Looks for the X-Google-Loop header, and if it exists, we fire up
    # Email::Address to ensure we strip out comments and descriptions.
    # Typical for googlegroups stuff.
    #
    if ( ( Email::Address->parse( $item->header( "X-Google-Loop" ) ) ) ) {
        foreach my $addr ( Email::Address->parse( $item->header( "X-Google-Loop" ) ) ) {
            my $listname = $addr->address;
            Logger( $epochtime, $score, "lists", $listname, $listname, $subject, "lists\/$listname/" );
            $listname = getlistname( $listname );
            $item->accept( "$MauchConfig::maildir/lists/$listname" );
        }
    } ## end if ( ( Email::Address->parse...

    # Looks for the List-Post header, and if it exists, we fire up
    # Email::Address to strip out extra comments and descriptions.  It also
    # usually appears with a mailto reference, so we strip that out too.
    #
    if ( ( Email::Address->parse( $item->header( "List-Post" ) ) ) ) {
        foreach my $addr ( Email::Address->parse( $item->header( "List-Post" ) ) ) {
            my $listname = $addr->address;
            Logger( $epochtime, $score, "lists", $listname, $listname, $subject, "lists\/$listname/" );
            $listname =~ s/mailto://;
            $listname = getlistname( $listname );
            $item->accept( "$MauchConfig::maildir/lists/$listname" );
        }
    } ## end if ( ( Email::Address->parse...

    # Tests for Sender email header, and if it exists - test for a string
    # that begings with owner.  If so, strip owner off, and we have our
    # listname.  Another common header with obscure list management softwares
    # so include it here as a last ditch resort.
    #
    if ( $item->header( "Sender" ) =~ /^owner-.*/ ) {
        my $listname = $item->header( "Sender" );
        Logger( $epochtime, $score, "lists", $listname, $listname, $subject, "lists\/$listname/" );
        $listname =~ s/owner-//;
        $listname = getlistname( $listname );
        $item->accept( "$MauchConfig::maildir/lists/$listname" );
    }
} ## end sub CheckLists

1;
